package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.AccountMapper;
import com.ruoyi.system.domain.Account;
import com.ruoyi.system.service.IAccountService;

/**
 * 用户信息Service业务层处理
 * 
 * @author 张英旺
 * @date 2024-02-26
 */
@Service
public class AccountServiceImpl implements IAccountService 
{
    @Autowired
    private AccountMapper accountMapper;

    /**
     * 查询用户信息
     * 
     * @param accountId 用户信息主键
     * @return 用户信息
     */
    @Override
    public Account selectAccountByAccountId(Long accountId)
    {
        return accountMapper.selectAccountByAccountId(accountId);
    }
    public Account selectAccountByUserId(Long userId)
    {
        return accountMapper.selectAccountByUserId(userId);
    }
    /**
     * 查询用户信息列表
     * 
     * @param account 用户信息
     * @return 用户信息
     */
    @Override
    public List<Account> selectAccountList(Account account)
    {
        return accountMapper.selectAccountList(account);
    }

    /**
     * 新增用户信息
     * 
     * @param account 用户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAccount(Account account)
    {
        int rows = accountMapper.insertAccount(account);
        insertSysUser(account);
        return rows;
    }

    /**
     * 修改用户信息
     * 
     * @param account 用户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAccount(Account account)
    {
        accountMapper.deleteSysUserByUserId(account.getAccountId());
        insertSysUser(account);
        return accountMapper.updateAccount(account);
    }

    /**
     * 批量删除用户信息
     * 
     * @param accountIds 需要删除的用户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAccountByAccountIds(Long[] accountIds)
    {
        accountMapper.deleteSysUserByUserIds(accountIds);
        return accountMapper.deleteAccountByAccountIds(accountIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param accountId 用户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAccountByAccountId(Long accountId)
    {
        accountMapper.deleteSysUserByUserId(accountId);
        return accountMapper.deleteAccountByAccountId(accountId);
    }

    /**
     * 新增用户信息信息
     * 
     * @param account 用户信息对象
     */
    public void insertSysUser(Account account)
    {
        List<SysUser> sysUserList = account.getSysUserList();
        Long accountId = account.getAccountId();
        if (StringUtils.isNotNull(sysUserList))
        {
            List<SysUser> list = new ArrayList<SysUser>();
            for (SysUser sysUser : sysUserList)
            {
                sysUser.setUserId(accountId);
                list.add(sysUser);
            }
            if (list.size() > 0)
            {
                accountMapper.batchSysUser(list);
            }
        }
    }
}
